package com.ms.p2p.controller.wap;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.SpringContextHolder;
import com.ms.p2p.Constants;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.entity.RetMap;
import com.ms.p2p.entity.content.Article;
import com.ms.p2p.entity.content.ArticleCategory;
import com.ms.p2p.provider.ArticleCategoryProvider;
import com.ms.p2p.provider.ArticleProvider;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chensk on 2017/11/28.
 */
@Controller
@RequestMapping({ "/wap/more" })
public class WapMoreController extends  WapBaseController{
    @Reference
    private ArticleProvider articleProvider;
    @Reference
    private ArticleCategoryProvider artileTypeProvider;

    /**
     * 获取
     */
    @RequestMapping({"/article"})
    public String more( Model model) {
        List<ArticleCategory> types = artileTypeProvider.getlist();
        model.addAttribute("types", types);
        return "/wap/more/more";
    }
    /**
     * 获取
     */
    @RequestMapping(value = {"/article/list/{cate_name}"},method = RequestMethod.GET)
    public String get(@PathVariable String cate_name, Model model) {
        ArticleCategory category = artileTypeProvider.getByCateName(cate_name);
        model.addAttribute("category", category);
        if(category.getType().equals("0")){
            return "wap/more/category_content";
        }
        return "wap/more/news";
    }
    @RequestMapping(value = {"/article/detail/{id}"},method = RequestMethod.GET)
    public String detail(@PathVariable String id, Model model) {
        Article article  = articleProvider.get(id);
        model.addAttribute("article", article);
        return "wap/more/article_content";
    }
    /**
     * 获取
     */
    @RequestMapping(value = {"/article/list/{cate_name}"},method = RequestMethod.POST)
    @ResponseBody
    public String list(@PathVariable String cate_name,
                       @RequestParam(value = "currentPage", defaultValue = "1", required = false)
                        Integer currentPage, Model model) {
        PageBean page = new PageBean();
        page.setPageSize(8);
        page.setCurrentPage(currentPage);
        ArticleCategory category = artileTypeProvider.getByCateName(cate_name);
        List<ArticleCategory> types = artileTypeProvider.getlist();
        Integer type_id = category.getId();
        List<Article> list = articleProvider.findPager(type_id, "1", page);
        page.setTotalCount(articleProvider.findCount(type_id, "1"));
        HashMap retMap = RetMap.Success("获取数据成功");

        FreeMarkerConfigurer freeMarkerConfigurer = SpringContextHolder.getBean("freeMarkerConfigurer");

        String templatePath = Constants.TEMPLATE_WAP_NEWS_ITEM;
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Template template = null;
        StringBuilder content = new StringBuilder();
        try {
            template = configuration.getTemplate(templatePath, "utf-8");
            HashMap loan = new HashMap();
            if(list.size()>0) {
                loan.put("articles", list);
                content.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, loan));
            }

        } catch (Exception e) {
            System.out.print(e);
            e.printStackTrace();
        }


        return  content.toString();
    }





}
